package com.luocc.ccdemo.widget

import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import com.luocc.ccdemo.R
import java.util.regex.Pattern

/**
 * 可以设置一些提示语放在edittext下面
 */
class EditTextController {
    private var selection = 0
    private var minLength = 0
    private var maxLength = 0
    private var changed = false
    private var avoidRegex: String? = null
    private var tips: String? = null
    private var textView: TextView? = null
    private var editText: EditText? = null
    private var expected: CharSequence = ""
    private var onStateChangedListener: OnStateChangedListener? = null
    private val textWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            selection = start + count
            s?.let {
                // 限制输入类型
                val str = filter(s)
                if (s.toString() != str.toString()) {
                    selection = start
                    changed = true
                    expected = str
                } else if (maxLength > 0 && s.length > maxLength) {
                    selection = start
                    changed = true
                    val sub1 = s.subSequence(0, start)
                    val sub2 = s.subSequence(start, s.length - 1)
                    expected = "$sub1$sub2"
                }
            }
        }

        override fun afterTextChanged(s: Editable?) {
            editText?.let {
                if (changed) {
                    changed = false
                    it.setText(expected)
                }
                it.setSelection(selection)
                onStateChangedListener?.onChanged(checkLength())
                if (maxLength > 0) {
                    it.postDelayed({
                        if (checkLength()) {
                            textView?.visibility = View.GONE
                            val right = ResourcesCompat.getDrawable(
                                it.resources,
                                R.mipmap.ic_appropriate,
                                null
                            )
                            right?.setBounds(0, 0, it.height / 2, it.height / 2)
                            it.setCompoundDrawables(null, null, right, null)
                        } else {
                            val right = ResourcesCompat.getDrawable(
                                it.resources,
                                R.mipmap.ic_warning,
                                null
                            )
                            right?.setBounds(0, 0, it.height / 2, it.height / 2)
                            if (tips != null) {
                                textView?.text = tips
                            }
                            textView?.setTextColor(Color.RED)
                            textView?.visibility = View.VISIBLE
                            it.setCompoundDrawables(null, null, right, null)
                        }
                    }, 200)
                }

            }

        }
    }

    fun setEditText(editText: EditText): EditTextController {
        this.editText = editText
        editText.addTextChangedListener(textWatcher)
        return this
    }

    fun setTextView(textView: TextView): EditTextController {
        this.textView = textView
        return this
    }

    fun setTips(tips: String): EditTextController {
        this.tips = tips
        return this
    }

    fun setMaxLength(max: Int): EditTextController {
        maxLength = max
        return this
    }

    fun setMinLength(min: Int): EditTextController {
        minLength = min
        return this
    }

    /**
     * 禁止输入的类型
     */
    fun setAvoidRegex(regex: String): EditTextController {
        avoidRegex = regex
        return this
    }

    fun setOnStateChangedListener(listener: OnStateChangedListener): EditTextController {
        onStateChangedListener = listener
        return this
    }

    private fun filter(s: CharSequence): CharSequence {
        if (avoidRegex == null) return s
        val compile = Pattern.compile(avoidRegex!!)
        val matcher = compile.matcher(s)
        return matcher.replaceAll("")
    }

    private fun checkLength(): Boolean {
        if (maxLength == 0) return true
        return editText?.length() in minLength until maxLength + 1
    }

    companion object {
        private const val TAG = "LC_PasswordController"
    }
}